home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14518 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c,comp.unix.programmer
  4. Subject: Re: Q: '\n' character
  5. Date: 15 Apr 1996 07:37:56 -0700
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4ktn04INNoev@keats.ugrad.cs.ubc.ca>
  8. References: <4kj66f$k0o@ren.cei.net> <4kmdsv$ojc@masala.cc.uh.edu> <4kmhpsINN7ak@keats.ugrad.cs.ubc.ca> <AD97189A966891F2@mcdiala02.it.luc.edu>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <AD97189A966891F2@mcdiala02.it.luc.edu>,
  12. Verne Arase <VArase@varase.it.luc.edu> wrote:
  13. >In article <4kmhpsINN7ak@keats.ugrad.cs.ubc.ca>,
  14. >c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) wrote:
  15. >
  16. > >Humor me as to why it is necessary to retain a whole string of data just
  17. >to
  18. > >then scan through it to find its length to remove the trailining newline,
  19. > >which was put there by a standard library function that already scanned
  20. >the
  21. > >characters once to _find_ the newline in the first place.
  22. >
  23. >This is a failing of the C standard I/O library; fgets() _ought_ to have
  24. >returned the length read.
  25.  
  26. Yes it should. A lot of the standard I/O library is braindead, but it had to be
  27. standardized a certain way to reflect existing practice and preserve the
  28. correctness of existing programs.
  29.  
  30. Having a return value that is the same as one of the arguments is a complete
  31. waste of a ``data path'' that could be used to return pertinent information.
  32.  
  33. Or better still: how about returning a pointer to the last character read! This
  34. would point to a null character if _no_ characters were read (thus indicating
  35. EOF on the attempt to read the first character), or to either a newline or a
  36. non-newline if at least one character was read. It would always be valid to
  37. dereference the returned pointer, provided a valid buffer was passed in. A use
  38. of fgets would then look like:
  39.  
  40.  
  41.     switch(*(ptr = nfgets(buf, sizeof buf, stdin))) {
  42.     case '\0':
  43.         /* EOF encountered                    */
  44.         break;
  45.     case '\n':
  46.         /* newline-terminated line read                */
  47.         len = ptr - buf;
  48.         *ptr = '\0';
  49.         break;
  50.     default:
  51.         len = ptr - buf + 1;
  52.         /* incomplete line read    or line incompletely read    */
  53.         break;
  54.     }
  55.  
  56. -- 
  57. I'm not really a jerk, but I play one on Usenet.
  58.